//
//  SYDDriveViewController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/7.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDDriveViewController.h"
#define groupInfoId @"SYDRegisterGroupCell"
#import "SYDRegisterView.h"
#import "SYDSelectCityViewController.h"
@interface SYDDriveViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)SYDRegisterHeaderView *registerHeaderView;
@property(nonatomic,strong)SYDDriveFooterView *driveFooterView;
@property (nonatomic, strong) UIImageView * imageView;

@property (nonatomic, strong) UIView *maskView;//遮罩View
@property (nonatomic, strong) SYDRegisterView *registerView;

@property(nonatomic,strong)NSString *carPhotoUrl;//人车合影照
@property(nonatomic,strong)NSString *busOwner;//车辆所有人
@property(nonatomic,strong)NSString *cityBelong;//所属城市
@property(nonatomic,strong)NSString *passengerCountPermitted;//荷载人数
@property(nonatomic,strong)NSString *busBrand;//车辆品牌
@property(nonatomic,strong)NSString *busLicensePlate;//车牌号
@property(nonatomic,strong)NSString *firmBelong;//所属公司

@end

@implementation SYDDriveViewController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationBar.title = @"注册";
    [self initDriveUI];
    [self navigationBarHandle];
}
-(void)initDriveUI{
    if (@available(iOS 11.0,*)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.carPhotoUrl = @"";
    self.busOwner = @"";
    self.cityBelong = @"";
    self.passengerCountPermitted = @"";
    self.busBrand = @"";
    self.busLicensePlate = @"";
    self.firmBelong = @"";
    self.tableView.tableHeaderView = self.registerHeaderView;
    self.tableView.tableFooterView = self.driveFooterView;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:groupInfoId bundle:nil] forCellReuseIdentifier:groupInfoId];
    
    WEAKSELF
    [[self.driveFooterView.groupBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        [SYDPhotoPickerManager shared].maxCount = 1;
        [[SYDPhotoPickerManager shared]showActionSheetInView:self.view fromController:self allowEdit:YES completion:^(UIImage *image) {
            UIImage *newImage =  [UIImage imageWithImage:image scaledToSize:CGSizeMake(330, 207)];
            [weakSelf.driveFooterView.groupBtn setImage:newImage forState:UIControlStateNormal];
            //上传图片
            [[SYDNetwork shareTool]uploadMultipartFilesWithParameters:nil mFile:@"mFile" uploadData:UIImageJPEGRepresentation(image, 0.01) success:^(NSURLSessionDataTask *task, id responseObject) {
                @try {
                    NSLog(@"responseObject:%@",responseObject);
                    if ([responseObject[@"statusCode"]integerValue] == 200) {
                        weakSelf.carPhotoUrl = responseObject[@"obj"];
                    }
                } @catch (NSException *exception) {
                    NSLog(@"exception:%@",exception.description);
                } @finally {
                }
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                NSLog(@"error:%@",error);
            }];
            [weakSelf.tableView reloadData];
        } cancelBlock:^{
            NSLog(@"cancel");
        }];
    }];
    
    // 点击遮罩层
    [self.maskView jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [weakSelf removeMaskOrPopup];
    }];
    self.registerView.block = ^{
        [weakSelf removeMaskOrPopup];
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    };
    self.driveFooterView.submitBtn.eventTimeInterval = 2.5;
    [[self.driveFooterView.submitBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self UploadDriveInfoRegisterToServer];
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SYDRegisterGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:groupInfoId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cityLabel.userInteractionEnabled = YES;
    WEAKSELF
    [cell.cityLabel jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        SYDSelectCityViewController *selectVc = [[SYDSelectCityViewController alloc]init];
        selectVc.selectCityBlock = ^(NSString *cityName, NSString *cityCode) {
           cell.cityLabel.text = cityName == nil?@"":cityName;
            weakSelf.cityBelong = cityName;
        };
        [self presentViewController:selectVc animated:YES completion:nil];
    }];
    return  cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 264;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

#pragma mark---lazy
-(UITableView *)tableView{
    if (!_tableView) {
        if (iPhone8_X) {
            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 88, SCREEN_WIDTH, SCREEN_HEIGHT - 88) style:UITableViewStylePlain];
        } else {
            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return  _tableView;
}
-(SYDRegisterHeaderView *)registerHeaderView{
    if (!_registerHeaderView) {
        _registerHeaderView = [[SYDRegisterHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 142)];
        _registerHeaderView.headerImage.image = [UIImage imageNamed:@"progress_bar4"];
        _registerHeaderView.titleMessageLbl.text = @"车辆信息";

    }
    return _registerHeaderView;
}
-(SYDDriveFooterView *)driveFooterView{
    if (!_driveFooterView) {
        _driveFooterView = [[SYDDriveFooterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
        [_driveFooterView.groupBtn setImage:[UIImage imageNamed:@"group_photo"] forState:UIControlStateNormal];
    }
    return _driveFooterView;
}
// 移除遮罩层和弹窗
- (void)removeMaskOrPopup{
    
    [self.registerView removeFromSuperview];
    
    [self.maskView removeFromSuperview];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (UIView *)maskView{
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:SCREEN_RECT];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0.3;
    }
    return _maskView;
}
-(SYDRegisterView *)registerView{
    if (!_registerView) {
        _registerView = [[SYDRegisterView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - 135, 190, 270, 212)];

    }
    return  _registerView;
}


-(void)UploadDriveInfoRegisterToServer{
    WEAKSELF
    
    self.busOwner = [YMSaveTool valueForKey:@"busOwner"];
    self.passengerCountPermitted = [YMSaveTool valueForKey:@"passengerCountPermitted"];
    self.busBrand = [YMSaveTool valueForKey:@"busBrand"];
    self.busLicensePlate = [YMSaveTool valueForKey:@"busLicensePlate"];
    self.firmBelong = [YMSaveTool valueForKey:@"firmBelong"];
    
    if ([self.cityBelong isEqualToString:@""]){
        [SVProgressHUD displayInfoWithStatus:@"请选择所属城市"];
    }else if ([self.busBrand isEqualToString:@""]){
        [SVProgressHUD displayInfoWithStatus:@"请填写车辆品牌和型号及颜色"];
    }else if ([self.busLicensePlate isEqualToString:@""]){
        [SVProgressHUD displayInfoWithStatus:@"请填写车牌号"];
    }else if ([self.passengerCountPermitted isEqualToString:@""]){
       [SVProgressHUD displayInfoWithStatus:@"请填写荷载人数"];
    }else if (![NSString deptNumInputShouldNumber:self.passengerCountPermitted]){
        [SVProgressHUD displayInfoWithStatus:@"请填写有效荷载人数"];
    }else if ([self.busOwner isEqualToString:@""]){
        [SVProgressHUD displayInfoWithStatus:@"请填写车辆所有人"];
    }else if ([self.carPhotoUrl isEqualToString:@""]){
        [SVProgressHUD displayInfoWithStatus:@"请填写人车合影照"];
    }else{

        [[SYDNetwork shareTool]userInfoRegisterWithRegisterStep:@"4" realName:@"" cardId:@"" iconUrl:@"" IDPhotoFrontUrl:@"" IDPhotoBackUrl:@"" busBrand:self.busBrand busLicensePlate:self.busLicensePlate busOwner:self.busOwner carPhotoUrl:self.carPhotoUrl cityBelong:self.cityBelong cityCode:[YMSaveTool valueForKey:CityCode] firmBelong:self.firmBelong passengerCountPermitted:self.passengerCountPermitted drivingLicenceStartTime:@"" drivinglicenceBackUrl:@"" drivinglicenceFrontUrl:@"" runningLicenceRegisterTime:@"" runningLicenceBackUrl:@"" runningLicenceFrontUrl:@"" userId:self.registerID success:^(NSURLSessionDataTask *task, id responseObject) {
            @try {
                NSLog(@"%@",responseObject);
                if ([responseObject[@"statusCode"]integerValue] == 200) {
                    //提交注册按钮
                    [weakSelf.view addSubview:self.maskView];
                    [weakSelf.view addSubview:self.registerView];
                }else{
                    [SVProgressHUD displayInfoWithStatus:responseObject[@"msg"]];
                }
            } @catch (NSException *exception) {
                NSLog(@"exception:%@",exception.description);
            } @finally {
                
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"error:%@",error);
            [SVProgressHUD displayInfoWithStatus:@"注册失败,请检查网络..."];
        }];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




